package com.cloudfun.campusshare.configuration.jpa.util;

import com.cloudfun.campusshare.configuration.jpa.restriction.support.*;
import com.cloudfun.campusshare.configuration.jpa.specification.SpecificationModel;

import javax.persistence.criteria.Predicate;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 王溪源
 * @ClassName: JpaRestrictionBuilder
 * @Description:
 **/
public class JpaRestrictionBuilder {
    private static Map<String, PredicateBuilder> predicateBuilderMap = new HashMap<> ();

    static {
        PredicateBuilder eqBuilder = new EqRestriction();
        PredicateBuilder neBuilder = new NeRestriction();
        PredicateBuilder geBuilder = new GeRestriction();
        PredicateBuilder gtBuilder = new GtRestriction();
        PredicateBuilder inBuilder = new InRestriction();
        PredicateBuilder llikeBuilder = new LLikeRestriction ();
        PredicateBuilder leBuilder = new LeRestriction ();
        PredicateBuilder likeBuilder = new LikeRestriction ();
        PredicateBuilder ltBuilder = new LtRestriction ();
        PredicateBuilder ninBuilder = new NinRestriction ();
        PredicateBuilder rlikeBuilder = new RLikeRestriction ();

        predicateBuilderMap.put (eqBuilder.getRestrictionName (), eqBuilder);
        predicateBuilderMap.put (neBuilder.getRestrictionName (), neBuilder);
        predicateBuilderMap.put (geBuilder.getRestrictionName (), geBuilder);
        predicateBuilderMap.put (gtBuilder.getRestrictionName (), gtBuilder);
        predicateBuilderMap.put (inBuilder.getRestrictionName (), inBuilder);
        predicateBuilderMap.put (llikeBuilder.getRestrictionName (), llikeBuilder);
        predicateBuilderMap.put (leBuilder.getRestrictionName (), leBuilder);
        predicateBuilderMap.put (likeBuilder.getRestrictionName (), likeBuilder);
        predicateBuilderMap.put (ltBuilder.getRestrictionName (), ltBuilder);
        predicateBuilderMap.put (ninBuilder.getRestrictionName (), ninBuilder);
        predicateBuilderMap.put (rlikeBuilder.getRestrictionName (), rlikeBuilder);
    }

    public static Predicate getRestriction(PropertyFilter filter, SpecificationModel model) {
        if (!predicateBuilderMap.containsKey (filter.getRestrictionName ())) {
            throw new IllegalArgumentException ("找不到约束名" + filter.getRestrictionName ());
        }
        PredicateBuilder predicateBuilder = predicateBuilderMap.get (filter.getRestrictionName ());
        return predicateBuilder.build (filter, model);
    }

    public static Predicate getRestriction(String propertyName, Object value, String restrictionName, SpecificationModel model) {
        if (!predicateBuilderMap.containsKey (restrictionName)) {
            throw new IllegalArgumentException ("找不到约束名" + restrictionName);
        }
        PredicateBuilder predicateBuilder = predicateBuilderMap.get (restrictionName);
        return predicateBuilder.build (propertyName, value, model);
    }

    public static Map<String, PredicateBuilder> getPredicateBuilderMap() {
        return predicateBuilderMap;
    }

    public static void setPredicateBuilderMap(Map<String, PredicateBuilder> predicateBuilderMap) {
        JpaRestrictionBuilder.predicateBuilderMap = predicateBuilderMap;
    }
}
